const app = getApp()
Page({
  data: {
    id: 0,
    shop:{},
    store:{},
    coupon:{},
    overlay:false,
    times:false,
    share:[],
    order:[],
    imgs:[]
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onLoad: function (options) {
    app.getParam(options, (rel) => {
      this.setData({
        id: rel.id,
        startBarHeight: app.globalData.startBarHeight,
        navgationHeight: app.globalData.navgationHeight,
      });
      this.getItem();
    });
  }, 
  /**
   * 获取商品信息
   */
  getItem: function () {
    //产品信息
    var param = {
      signkey: app.util().getRandom(12),
      id: this.data.id
    }
    app.api().Get('api/v4/allwin/shop/item',param,(result) => {
      this.setData({
        shop: result.data.item,
        store: result.data.store
      });
      if(result.data.item.coupon_id > 0){
        var coupon = {
          signkey: app.util().getRandom(12),
          id: result.data.item.coupon_id,
        }
        app.api().Get('api/v4/allwin/coupon/getCoupon',coupon,(result) => {
          if (result.code == 200) {
            this.setData({
              coupon: result.data,
            });
          }
        })
      }
    })
    //增加分享用户
    var share = {
      id: this.data.id,
      signkey: app.util().getRandom(12)
    }
    app.api().Get('api/v4/allwin/shop/itemOrderUser',share,(result) => {
      if(result.code == 200){
        this.setData({
          share: result.data.share,
        });
        setTimeout(()=>{
          this.setData({
            times: result.data.order.create_time <= 0 ? false : true,
            order: result.data.order,
          });
        },1200)
      }
    })
  },
  //购买
  onPayment:function () {
    wx.navigateTo({
      url:'on_order?id='+this.data.id
    })
  },
  //重新支付
  restOrder:function () {
    var param = {
      signkey: app.util().getRandom(12),
      shop_id: this.data.id
    }
    this.onHideOrder()
    wx.showLoading({
      title:'加载中'
    })
    app.api().Post('api/v4/allwin/shopdopay/index',param,(res) => {
      if (res.code == 200) {
        wx.hideLoading()
       app.doWechatPay(res.data,()=>{
          wx.navigateTo({
            url: '../order/index?active=2',
          })
       },()=>{
        app.wxAlert('你取消了支付')
       })
      }
    })
  },
  //预览订单
  viewOrder:function () {
    wx.navigateTo({
      url: '../order/viewOrder?id='+this.data.order.id,
    })
  },
  //分享按钮
  onShareAppMessage: function () {
    var shop = this.data.shop;
    return {
      title: shop.name,
      desc: shop.title,
      path: '/pages/shop/item?id=' + shop.id + '&ucode=' + app.globalData.loginuser.ucode,
      imageUrl: shop.img
    }
  },
  onClickHide() {
    this.setData({
      overlay: !this.data.overlay
    });
  },
  onHideOrder() {
    this.setData({
      times:false
    });
  },
  //改变时间
  onChange(e) {
    this.setData({
      timeData: e.detail
    });
  },
  //结束倒计时
  finished(e) {
    var shop = this.data.shop;
        shop.end_time = 0;
    this.setData({
      shop:shop
    });
  },
  //返回上一页
  navigateBack(){
    let pages = getCurrentPages();
    if(pages.length == 1){
      wx.switchTab({
        url: '../index',
      })
    }else{
      wx.navigateBack({
        delta: 1
      })
    }
  }
})